Get started creating Rules. Technical details about how to use Algolia Rules.
alternatives
setting of a Rule is false
, the condition must match the entire word (fully and exactly) for the Rule to be triggered.enabled
flag to false
. Rules that have been turned off remain in the index, meaning they’re still searchable, but ignored at query time. This is an alternative to deleting a Rule when you wish to turn it off temporarily.
For even more fine-grained control, you may specify validity windows on Rules (via the validity
attribute). Rules with validity windows only apply during the specified time frames: they’re ignored the rest of the time. Rules with no time windows always apply. Like with the enabled
flag, Rules with validity windows remain searchable all the time.
query
search parameter.filters
or facetFilters
, but not numericFilters
, tagFilters
, nor optionalFilters
.ruleContexts
parameteralternatives
setting is true
, then matching considers typo tolerance, plurals, and synonyms. If you set ignorePlurals
to true
or synonyms
to false
, your alternatives
setting is ignored.attributesForFaceting
.
Matching is case insensitive. Contrary to literals, facet values may be phrases.
The pattern is implicitly a phrase. The order of words matters: foo bar
and bar foo
aren’t identical patterns.
The pattern also has an anchoring type,
depending on whether its boundaries (beginning, end) must coincide with the boundaries of the query string:
:
), or any special character (}
, {
, \
), you need to escape the character.
For example, to set your query pattern to “thrones: episode”, you need to escape the :
(“thrones\:episode”). The first \
is for JSON and the second is for the Rules engine.
filters
or facetFilters
can trigger Rules. The primary goal of this condition is to create Rules that are triggered on specific category pages or when a user applies specific filters.
If a Rule’s condition has only filters and not query pattern nor context, the engine applies the Rule’s consequences. The engine applies the Rule’s consequences if a condition only has filters: no query patterns or context. If a condition also has query patterns or context, those must match for the Rule to be triggered.
attributesForFaceting
.brand:Nike
as its conditionbrand:Nike
and the query “shoes” as its conditionbrand:Nike
is applied, only Rule B is triggered.brand:Nike
is applied, only Rule A is triggered."filters": "brand:Nike"
, this Rule triggers if only the value Nike
is selected in the filter brand
. The engine doesn’t trigger the Rule for a query filtered on brand:Nike OR brand:Supreme
.
If a condition has multiple filters, all applied filters must match exactly to trigger the Rule. For example, if the Rule condition is "filters": "brand:Nike AND color:red"
, then the engine triggers this Rule if only the value Nike
is selected in the brand
filter and only the value red
is selected in the color
filter. The Rule isn’t triggered if only Nike
is selected without red
, and it doesn’t trigger if brand:Nike
and color:red OR color:blue
are selected.
brand:Nike
and no other filters, the engine only considers the brand
attribute’s applied filter values to decide whether to trigger the Rule.
For example, if a query filters on brand:Nike AND color:red
, that Rule doesn’t consider the value of the color
filter because it’s not part of the condition. The engine triggers the Rule since brand
is filtered on Nike
.
OR
conditionAND
condition.brand:Supreme
, brand:Nike
, gender:men
as (brand:Supreme OR brand:Nike) AND (gender:men)
.
In other words, the engine triggers the Rule if a user selects exactly the values (Supreme
, Nike
and men
) in the filters on the frontend.
These conditions only hold if the frontend filters work with the same logic. For example, if a user filters on Nike
, Supreme
for brand, and red
for color, the filtering logic follows the AND of ORs
pattern: (brand:Nike OR brand:Supreme) AND color:red
. Thus the filters are compatible with filtering conditions possible on Rules.
Filters in Rules’ conditions aren’t compatible with ORs of ANDs
, ANDs of ANDs
or ORs of ORs
logic. For example, imagine a real estate search where a user filters for a two-bedroom apartment in Paris with a balcony and fireplace. In this use case, the desired logic would be ANDs of ANDs
: bedrooms:2 AND (amenities:balcony AND amenities:fireplace) AND location:Paris
. You can’t create a Rule with a condition to match this filtering.
ruleContexts
parameter.
The contexts found in the ruleContexts
of user searches are compared to the contexts you assign to your Rules.
Contexts passed as parameters during your search: doing so activates all Rules that share the context you passed. Activation means the Rule is considered not to have been applied. If your Rule’s condition only contains a context, the Rules consequence is applied to search results. If your Rule also contains a query pattern, that pattern must match for the consequence to be applied.
For example, you can define a set of Rules with the context “homepage” and pass the same context value along with any search that originates from your homepage. Now you have a set of Rules that apply to the homepage search, giving a more customized experience.
A Rule can have one context at most, but a search may have multiple contexts: these are treated as disjunctive (OR).
When one or more contexts are specified at query time, Rules matching any of those contexts are activated.
General Rules are always activated.
The context’s primary goal is to conditionally enable Rules for specific search environments.
If present, the context is a string that must be passed at query time in the ruleContexts
search parameter
with the same value for the Rule to be triggered. Matching is case-sensitive.
The ruleContexts
parameter allows you to enable multiple contexts simultaneously.
search
as part of its scope can be changed at query time.
One common example of a parameter that is useful to change at query time is filters
. For example, when someone searches “tablet”, you want to show not only tablets (products with ‘tablets’ in their categories
attribute) but also laptops used as tablets (products with ‘hybrids’ in their categories
attribute). To do this in the dashboard, click Add Query Parameter when adding a consequence for your Query Rule, and add the following JSON into the editor:
AND
, default) or disjunctive (OR
). By default, the filters are combined with an AND
operator. However, if you specify the filter to be
disjunctive, an OR
operator is used instead. In both cases, relationships to other filters
in the query will always be conjunctive (AND
).
You can also specify a score for the created filter, mainly useful for optional facet filters (see below).
For example, to automatically filter on the brand
facet, using OR
in case of multiple occurrences:
negative
field and set it to true
.
automaticFacetFilters
, but the filters are optional.
Behaves like optionalFilters
.
foo bar
, the search engine should remove the word foo
and the word bar
should be replaced with baz
:
query
parameter, as you would do for any
other search parameters:
objectID
, are promoted to specific positions in the results list or removed from them. This promotion works seamlessly with Algolia’s built-in pagination, mingling promoted hits with regular
hits and removing hidden hits while keeping pages the same size.
Hit promotion is detailed below.
userData
array. If multiple Rules are applied,
each set of user data is appended to this array. User data can be used to display specific information that doesn’t affect pagination.
objectID
.
A promoted object will always be considered a hit, even if it doesn’t match the query.
If it would have matched the query, it’s removed from its original position and inserted at its promoted position,
even if the original position would have been better than the promoted position. In other words, promoted hits can also be “demoted”.
For performance reasons, promoted positions are restricted to the range [0, 300] (keep in mind that positions are zero-based).
Inside the same Rule, each promoted object must have a different promoted position.
If promoted objects from two distinct Rules are triggered for the same query: